package com.swas.service.impl;

import com.swas.dao.BuyBackDao;
import com.swas.dao.BuyCheckDao;
import com.swas.dao.BaseStockDao;
import com.swas.entity.BaseStock;
import com.swas.entity.BaseStockExample;
import com.swas.entity.BuyBack;
import com.swas.entity.BuyCheck;
import com.swas.service.BuyCheckService;
import com.swas.vo.BaseStockVO;
import com.swas.vo.BuyCheckVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxysky
 */
@Transactional
@Service
public class BuyCheckServiceImpl implements BuyCheckService {

    @Autowired
    private BuyCheckDao buyCheckDao;

    @Autowired
    private BuyBackDao buyBackDao;

    @Autowired
    private BaseStockDao baseStockDao;

    @Override
    public List<BuyCheckVO> selectAllNo() {
        return buyCheckDao.selectAllNo();
    }

    @Override
    public List<BuyCheckVO> selectAll() {
        return buyCheckDao.selectAll();
    }

    @Override
    public List<BuyCheckVO> searchLikeNo(String name, String no) {
        return buyCheckDao.searchLikeNo(name, no);
    }

    @Override
    public List<BuyCheckVO> searchLike(String name, String checkdate) {
        return buyCheckDao.searchLike(name, checkdate);
    }

    @Override
    public boolean updateCheck(Integer id, String checkman) {
        BuyCheckVO buyCheckVO = new BuyCheckVO();
        buyCheckVO.setId(id);
        buyCheckVO.setCheckdate(new Date());
        buyCheckVO.setIschecked("已审核");
        buyCheckVO.setCheckman(checkman);
        return buyCheckDao.updateByPrimaryKeySelective(buyCheckVO) > 0;
    }

    @Override
    public boolean addCheck(BuyCheckVO buyCheckVO) {
        buyCheckVO.setCheckdate(null);
        buyCheckVO.setIschecked("未审核");
        buyCheckVO.setCheckman(null);
        BaseStockExample example = new BaseStockExample();
        BaseStock baseStock = baseStockDao.selectByPrimaryKey(buyCheckVO.getStockid());
        BaseStock bs = baseStockDao.selectByPrimaryKey(buyCheckVO.getStockid());
        if (bs == null){
            example.createCriteria().andNameEqualTo(buyCheckVO.getName());
            List<BaseStock> baseStocks = baseStockDao.selectByExample(example);
            buyCheckVO.setStockid(baseStocks.get(0).getId());
        } else {
            example.createCriteria().andNameEqualTo(baseStock.getName());
            List<BaseStock> baseStocks = baseStockDao.selectByExample(example);
            buyCheckVO.setStockid(baseStocks.get(0).getId());
        }
        return buyCheckDao.insertSelective(buyCheckVO) > 0;
    }

    @Override
    public List<BaseStockVO> selectBuyCheckAll() {
        List<BaseStockVO> list = buyCheckDao.selectBuyCheckAll();
        return list;
    }

    @Override
    public List<BaseStockVO> selectLike(String name, String buyman) {
        List<BaseStockVO> list = buyCheckDao.selectLike(name,buyman);
        return list;
    }

    @Override
    public BuyBack getOne(Integer id) {
        return buyBackDao.selectByPrimaryKey(id);
    }

    @Override
    public boolean setOne(Integer id, String dec) {
        return buyCheckDao.setOne(id, dec);
    }


}
